/*
 *
 * $Id$
 *
 * UMKC Robotics 2009
 *
 * All direct interaction with the ATMega 128 should go
 * into this file.  No static - so the linker can find
 * these functions when needed.  Also allows you to 
 * completely bypass the io api if you so desire.
 *
 *  Authors:  Jared Bayne, Ryan Boyd
 * 
 */

/* spoof -mmcu=atmega128 so the avr symbols will be
 * included correctly when compiling with your build
 * machine's libc.  This will eventually cause problems
 * (for compiling w/ system libc ). */
#if defined(WITH_SYSLIBC)
#  define __AVR_ATmega128__
#endif

#include <avr/io.h>
/*#include <avr/interrupt.h> */



/* this function initializes all ports (this is taken from
 * Initialize.h for the 09 robot). 
 *
 * We *could* do all of this using BotIo and BotIoctl as that
 * would entirely seperate 'runtime' code from direct interaction
 * from the microcontroller.  But, that seems like overkill. */
void mega128_init() {

	
	/* Port A initialization
        %1100 0000 (1=OUT 0=IN)
        Buttons on Development Board
        PA6 = onboard LED
        */
        PORTA=0x00;
        DDRA=0xC0; // 192/255
        
        /* Port B initialization
        %1111 1111 (1=OUT 0=IN)
        PB0 = CPU2Add6
        PB2 = M2C1
        PB3 = CPU3Add1
        PB4 = M1C1
        PB5 = M1C2
        PB6 = M2C2
        
        */
        PORTB=0x00;
        DDRB=0xFF;   // 255/255

        /* Port C initialization
        %1111 1111 (1=OUT 0=IN)
        LCD Screen on Development Board
        PC3 = CPU3Add2
        */
        PORTC=0x00;
        DDRC=0xFF;

        /*Port D Initilializtion
        %1111 1001  (1=OUT 0=IN)
        PD1 = CPU2Data
        PD2 = RXD1       // reciever
        PD3 = TXD1   // transmiter
        PD4 = CPU2Add3
        PD5 = CPU2Add4  
        */
        PORTD=0x00;
        /*DDRD=0b11111011; */
        DDRD = 0xFB;
        
        /* Port E initialization
        %1000 1111 (1=OUT 0=IN)
        PE2 = CPU2ADD1
        PE3 = CPU2Add2
        PE7 = CPU3Add4
        */
        PORTE=0x00;    // buzzer
        /*DDRE=0b11001111;*/                // buzzer 143/255
        DDRE = 0xCF;

        /*Port F initialization
        %1111 1111 (1=OUT 0=IN)
        nothing is used on port F right now
        it can be used for JTAG programming but we use bootloader programming right now. */
        PORTF=0x00; 
        DDRF=0xF0;

        /* Port G initialization
        %1111 1111 (1=OUT 0=IN)
        nothing is used on port G right now */
        PORTG=0x00;
        DDRG=0x00;



}


